<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
<!--      <el-form-item label="学号" prop="stuSno">-->
<!--        <el-input-->
<!--          v-model="queryParams.stuSno"-->
<!--          placeholder="请输入学号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="姓名" prop="stuName">
        <el-input
          v-model="queryParams.stuName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="专业学科" prop="subId">
        <el-select v-model="queryParams.subId" placeholder="专业学科" filterable clearable @keyup.enter.native="handleQuery"
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in subjectList" :key="index" :label="item.subName"
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="性别" prop="stuGender">
        <el-select v-model="queryParams.stuGender" placeholder="请输入性别" clearable @keyup.enter.native="handleQuery"
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in stuGenderOptions" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="考生号" prop="stuExamId">-->
<!--        <el-input-->
<!--          v-model="queryParams.stuExamId"-->
<!--          placeholder="请输入考生号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="证件号码" prop="stuCardId">-->
<!--        <el-input-->
<!--          v-model="queryParams.stuCardId"-->
<!--          placeholder="请输入证件号码"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="毕业年月" prop="stuGraduateDate">-->
<!--        <el-input-->
<!--          v-model="queryParams.stuGraduateDate"-->
<!--          placeholder="请输入毕业年月"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      ：0未审核 1审核中 2 审核未通过 3审核通过-->
      <el-form-item label="审核状态" prop="examineStatus" >
        <el-select v-model="queryParams.examineStatus" placeholder="请选择审核状态" clearable :style="{width: '100%'}">
          <el-option v-for="item in examineStatus" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="是否选择导师" prop="isChoose">
        <el-select v-model="queryParams.isChoose" placeholder="是否选择导师" clearable @keyup.enter.native="handleQuery" :style="{width: '100%'}">
          <el-option label="未选择" :value="0" />
          <el-option label="已选择" :value="1" />
        </el-select>
      </el-form-item>


      <el-form-item style="margin-left: 70px;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mentor:student:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mentor:student:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mentor:student:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mentor:student:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false"/>
<!--      <el-table-column label="学号" align="center" prop="stuSno" />-->
      <el-table-column label="姓名" align="center" prop="stuName"  />
      <el-table-column label="性别" align="center" prop="stuGender">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.stuGender==0">女</el-tag>
          <el-tag v-if="scope.row.stuGender==1">男</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="专业学科" align="center" prop="subName" width="200" >
        <template slot-scope="scope">
          <el-tag type="warning">{{scope.row.subName}}</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="证件号码" align="center" prop="stuCardId" />-->
<!--      <el-table-column label="出生日期" align="center" prop="stuBirth" />-->
<!--      <el-table-column label="毕业年月" align="center" prop="stuGraduateDate" />-->
      <el-table-column label="审核状态" align="center" prop="examineStatus">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.examineStatus==0">未审核</el-tag>
          <el-tag v-if="scope.row.examineStatus==1">审核中</el-tag>
          <el-tag type="danger" v-if="scope.row.examineStatus==2">审核未通过</el-tag>
          <el-tag type="success" v-if="scope.row.examineStatus==3">审核通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否已经选择导师" align="center" prop="isChoose">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isChoose==0" type="danger">未选择</el-tag>
          <el-tag v-if="scope.row.isChoose==1" type="success">已选择</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="350">
        <template slot-scope="scope">

          <el-button
            v-if="scope.row.isChoose==0&&scope.row.examineStatus==3"
            size="small"
            type="primary"
            @click="adminHelpChooseInstructor(scope.row.id)"
            v-has-role="['admin']"
          >选择导师</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-more"
            @click="toStudentInfo(scope.row.id)"
            v-has-role="['admin']"
          >查看详细信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mentor:student:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mentor:student:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改研究生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="stuName">
          <el-input v-model="form.stuName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="stuGender">
          <el-select v-model="form.stuGender" placeholder="请输入性别" clearable @keyup.enter.native="handleQuery"
                     :style="{width: '100%'}">
            <el-option v-for="(item, index) in stuGenderOptions" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考生号" prop="stuExamId">
          <el-input v-model="form.stuExamId" placeholder="请输入考生号" />
        </el-form-item>
        <el-form-item label="证件号码" prop="stuCardId">
          <el-input v-model="form.stuCardId" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="出生日期" prop="stuBirth">
          <el-input v-model="form.stuBirth" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="毕业年月" prop="stuGraduateDate">
          <el-input v-model="form.stuGraduateDate" placeholder="请输入毕业年月" />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="form.examineStatus" placeholder="请选择审核状态" clearable size="small">
            <el-option v-for="item in examineStatus" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择导师" :visible.sync="selectIns.visible" width="500px" @close="handleSelectClose" append-to-body>
      <el-form :model="selectIns.form">
        <el-form-item label="选择导师">
          <el-select filterable clearable v-model="selectIns.form.insId" @change="handleSelectChange">
            <el-option v-for="ins in selectIns.insList" :value="ins.insId" :label="ins.insName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="选择方向">
          <el-select filterable clearable v-model="selectIns.form.subId" :disabled="!selectIns.form.insId">
            <el-option v-for="subject in selectIns.subjectList" :value="subject.subId" :label="subject.subName+'\t\t'+subject.checkedNumber+'/'+subject.pgNumber"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div>
        <el-button type="primary" @click="handleSelectIns" :disabled="selectIns.form.subId==null||selectIns.form.insId==null||selectIns.form.stuId==null">确 定</el-button>
        <el-button @click="handleSelectClose">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent, exportStudent } from "@/api/mentor/student";
import { getAllSubject } from "@/api/mentor/subject";
import {selectNotFull} from "../../../api/mentor/instructor";
import {chooseIns} from "../../../api/mentor/student";
export default {
  name: "Student",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 研究生信息表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuSno: undefined,
        stuName: undefined,
        stuGender: undefined,
        stuExamId: undefined,
        stuCardId: undefined,
        stuBirth: undefined,
        stuGraduateDate: undefined,
        examineStatus: undefined,
        subId:undefined,
        isChoose:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      },
      examineStatus:[
        {
          value:0,
          label:'未审核'
        },
        {
          value:1,
          label:'审核中'
        },
        {
          value:2,
          label:'审核未通过'
        },
        {
          value:3,
          label:'审核已通过'
        }
      ],
      stuGenderOptions: [{
        "label": "女",
        "value": 0
      }, {
        "label": "男",
        "value": 1
      }],
      subjectList:[],
      selectIns:{
        visible:false,
        insList:undefined,
        form:{
          insId:undefined,
          subId:undefined,
          stuId:undefined
        },
        subjectList:[]

      }
    };
  },
  created() {
    this.getList();
    this.getAllSubject()
  },
  methods: {
    /** 查询研究生信息列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        stuSno: undefined,
        stuName: undefined,
        stuGender: undefined,
        stuExamId: undefined,
        stuCardId: undefined,
        stuBirth: undefined,
        stuGraduateDate: undefined,
        examineStatus: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加研究生信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改研究生信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除研究生信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStudent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有研究生信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStudent(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    toStudentInfo(studentId){
      //跳转到学生详细信息页面
      this.$router.push(`/student/detail/${studentId}`)
    },
    async getAllSubject() {
      const res = await getAllSubject()
      this.subjectList = res.data
      // console.log(res)
    },
    async adminHelpChooseInstructor(stuId) {
      this.selectIns.form.stuId = stuId
      //管理员指定导师进行选择
      const res = await selectNotFull()
      this.selectIns.insList = res.data
      // console.log(res.data)
      this.selectIns.visible = true
    },
    handleSelectChange(insId){
      if (insId==null||insId=='')return
      let curInsInfo = this.selectIns.insList.filter(item=>item.insId ==insId)
      this.selectIns.subjectList = curInsInfo[0].children
    },
    handleSelectClose(){
      this.selectIns.visible = false
      this.selectIns.subjectList = []
      this.selectIns.insList = []
      this.selectIns.form = {}
    },
    async handleSelectIns() {
      const res = await chooseIns(this.selectIns.form.insId,this.selectIns.form.stuId,this.selectIns.form.subId,2)
      if (res.code!=200)return this.$message.error(res.msg)
      this.$message.success("选择成功")
      this.selectIns.visible = false
      this.getList()
    }
  }
};
</script>
